package com.ruoyi.service.impl;

import java.util.List;
import java.util.Map;

import com.ruoyi.app.dto.QrCodeDto;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.forest.WeChatClient;
import com.ruoyi.framework.config.SmartParkingCloudConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mapper.ParkingLaneMapper;
import com.ruoyi.domain.ParkingLane;
import com.ruoyi.service.IParkingLaneService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * 车道信息Service业务层处理
 *
 * @author dongdong
 * @date 2025-08-30
 */
@Service
public class ParkingLaneServiceImpl implements IParkingLaneService {
    @Resource
    private ParkingLaneMapper parkingLaneMapper;
    @Autowired
    private WeChatClient weChatClient;

    /**
     * 查询车道信息
     *
     * @param id 车道信息主键
     * @return 车道信息
     */
    @Override
    public ParkingLane selectParkingLaneById(Long id)
    {
        return parkingLaneMapper.selectParkingLaneById(id);
    }

    /**
     * 查询车道信息列表
     *
     * @param parkingLane 车道信息
     * @return 车道信息
     */
    @Override
    @DataScope(deptAlias = "pl")
    public List<ParkingLane> selectParkingLaneList(ParkingLane parkingLane)
    {
        return parkingLaneMapper.selectParkingLaneList(parkingLane);
    }

    /**
     * 新增车道信息
     *
     * @param parkingLane 车道信息
     * @return 结果
     */
    @Override
    public int insertParkingLane(ParkingLane parkingLane)
    {
        parkingLane.setCreateTime(DateUtils.getNowDate());
        return parkingLaneMapper.insertParkingLane(parkingLane);
    }

    /**
     * 修改车道信息
     *
     * @param parkingLane 车道信息
     * @return 结果
     */
    @Override
    public int updateParkingLane(ParkingLane parkingLane)
    {
        parkingLane.setUpdateTime(DateUtils.getNowDate());
        return parkingLaneMapper.updateParkingLane(parkingLane);
    }

    /**
     * 批量删除车道信息
     *
     * @param ids 需要删除的车道信息主键
     * @return 结果
     */
    @Override
    public int deleteParkingLaneByIds(Long[] ids)
    {
        return parkingLaneMapper.deleteParkingLaneByIds(ids);
    }

    /**
     * 删除车道信息信息
     *
     * @param id 车道信息主键
     * @return 结果
     */
    @Override
    public int deleteParkingLaneById(Long id)
    {
        return parkingLaneMapper.deleteParkingLaneById(id);
    }

    @Override
    public List<Map<String, Object>> selectList() {
        return parkingLaneMapper.selectList();
    }

    @Override
    public void getInSideCode(ParkingLane parkingLane, HttpServletResponse response) throws Exception {
        QrCodeDto qrCode = new QrCodeDto();
        qrCode.setPage("pages/index/scanInsideCode");
        qrCode.setScene(String.valueOf(parkingLane.getParkId()));
        qrCode.setEnv_version("release"); // 正式版为 "release"，体验版为 "trial"，开发版为 "develop"
        Map<String, Object> token = weChatClient.getWxToken(SmartParkingCloudConfig.APPID_XCX, SmartParkingCloudConfig.SECRET_XCX);
        byte[] byteRes = weChatClient.getWxQrCode(token.get("access_token").toString(), qrCode);
        response.setContentType("image/png");
        response.setContentLength(byteRes.length);
        response.getOutputStream().write(byteRes);
    }
    @Override
    public void getInOutCode(ParkingLane parkingLane, HttpServletResponse response) throws Exception {
        QrCodeDto qrCode = new QrCodeDto();
        qrCode.setPage("pages/index/scanInOutCode");
        qrCode.setScene(parkingLane.getParkId()+"-"+parkingLane.getQrCodeId());
        qrCode.setEnv_version("release"); // 正式版为 "release"，体验版为 "trial"，开发版为 "develop"
        Map<String, Object> token = weChatClient.getWxToken(SmartParkingCloudConfig.APPID_XCX, SmartParkingCloudConfig.SECRET_XCX);
        byte[] byteRes = weChatClient.getWxQrCode(token.get("access_token").toString(), qrCode);
        response.setContentType("image/png");
        response.setContentLength(byteRes.length);
        response.getOutputStream().write(byteRes);
    }
}
